package com.spi.soconversion.base.definition;

import java.math.BigDecimal;

import com.spi.soconversion.util.SOCConstants;
import com.spi.soconversion.util.SOCUtility;
import com.spi.soconversion.util.XMLAttributeConstants;
import com.spi.soconversion.util.XMLTagConstants;

public class SOCOrderLine {
	
	private String lineShipToKey;
	private String deliveryMethod;
	private String fulfillmentType;
	private String itemGroupCode;
	private String lineType;
	private String minShipByDate;
	private String orderedQty;
	private String orgOrderedQty;
	private String pricingDate = SOCConstants.EMPTY_STRING;
	private int primeLineNo;
	private String promisedEndDate;
	private BigDecimal otherCharges;
	private String promisedStartDate;
	private String reqDeliveryDate;
	private String reqShipDate;
	private String shipNode;
	private String shipToID;
	private String timeZone;
	private String shippingMethod;
	private String status;
	private SOCOrderLineItem item = new SOCOrderLineItem();
	private SOCOrderLinePriceInfo priceInfo = new SOCOrderLinePriceInfo();
	private SOCOrderPersonalInfoShipTo infoShipTo = new SOCOrderPersonalInfoShipTo();
	private SOCOrderNotes lineNotes = new SOCOrderNotes();
	private SOCOrderLineExtn lineExtn = new SOCOrderLineExtn();
	private SOCOrderLineStatus lineStatus = new SOCOrderLineStatus();
//	private SOCOrderLineSchedule lineschedule = new SOCOrderLineSchedule();
//	public SOCOrderLineSchedule getLineschedule() {
//		return lineschedule;
//	}
//
//	public void setLineschedule(SOCOrderLineSchedule lineschedule) {
//		this.lineschedule = lineschedule;
//	}

	private SOCOrderLineTaxes lineTaxes = new SOCOrderLineTaxes();
	private SOCOrderLineSalvage salvageOrderLine = new SOCOrderLineSalvage();
	private BigDecimal lineTotal;
	private BigDecimal lineTotalTax;
	private boolean isReturned = false;
	private String lineId;
	private String itemType;
	private BigDecimal unitListPrice;
	private String serviceRefId;
	private String lineCategory;
	private SOCOrderLineCharges lineCharges = new SOCOrderLineCharges();
	private String itemCategory;
	
	public String getLineShipToKey() {
		return SOCUtility.checkNull(lineShipToKey);
	}
	
	public void setLineShipToKey(String lineShipToKey) {
		this.lineShipToKey = lineShipToKey;
	}
	
	public String getLineCategory() {
		return lineCategory;
	}

	public void setLineCategory(String lineCategory) {
		this.lineCategory = lineCategory;
	}
	
	public String getServiceRefId() {
		return serviceRefId;
	}

	public void setServiceRefId(String serviceRefId) {
		this.serviceRefId = serviceRefId;
	}
	
	public BigDecimal getUnitListPrice() {
		return SOCUtility.checkNumberNull(unitListPrice);
	}

	public void setUnitListPrice(BigDecimal unitListPrice) {
		this.unitListPrice = unitListPrice;
	}
	
	public String getLineItemType() {
		return itemType;
	}

	public void setLineItemType(String itemType) {
		this.itemType = itemType;
	}
	
	public String getLineId() {
		return lineId;
	}

	public void setLineId(String lineId) {
		this.lineId = lineId;
	}
	
	public void setIsReturned(boolean returnedLine)
	{
		this.isReturned = returnedLine;
	}
	
	public boolean getIsReturned()
	{
		return isReturned;
	}
	
	public String getDeliveryMethod() {
		return deliveryMethod;
	}

	public void setDeliveryMethod(String deliveryMethod) {
		this.deliveryMethod = deliveryMethod;
	}
	
	public String getFulfillmentType() {
		return fulfillmentType;
	}

	public void setFulfillmentType(String fulfillmentType) {
		this.fulfillmentType = fulfillmentType;
	}
	
	public void setItemGroupCode(String itemGroupCode) {
		this.itemGroupCode = itemGroupCode;
	}

	public String getItemGroupCode() {
		return itemGroupCode;
	}
	
	public String getLineType() {
		return lineType;
	}

	public void setLineType(String lineType) {
		this.lineType = lineType;
	}
	
	public String getMinShipByDate() {
		return minShipByDate;
	}

	public void setMinShipByDate(String minShipByDate) {
		this.minShipByDate = minShipByDate;
	}
	
	public String getOrderedQty() {
		return orderedQty;
	}

	public void setOrderedQty(String orderedQty) {
		this.orderedQty = orderedQty;
	}
	
	public void setOrgOrderedQty(String orgOrderedQty) {
		this.orgOrderedQty = orgOrderedQty;
	}

	public String getOrgOrderedQty() {
		return orgOrderedQty;
	}
	
	public void setOtherCharges(BigDecimal otherCharges) {
		this.otherCharges = otherCharges;
	}
	
	public BigDecimal getOtherCharges() {
		return SOCUtility.checkNumberNull(otherCharges);
	}
	
	public String getPricingDate() {
		return pricingDate;
	}

	public void setPricingDate(String pricingDate) {
		this.pricingDate = pricingDate;
	}
	
	public int getPrimeLineNo() {
		return primeLineNo;
	}

	public void setPrimeLineNo(int primeLineNo) {
		this.primeLineNo = primeLineNo;
	}
	
	public void setPromisedEndDate(String promisedEndDate) {
		this.promisedEndDate = promisedEndDate;
	}

	public String getPromisedEndDate() {
		return promisedEndDate;
	}
	
	public String getPromisedStartDate() {
		return promisedStartDate;
	}

	public void setPromisedStartDate(String promisedStartDate) {
		this.promisedStartDate = promisedStartDate;
	}
	
	public String getReqDeliveryDate() {
		return reqDeliveryDate;
	}

	public void setReqDeliveryDate(String reqDeliveryDate) {
		this.reqDeliveryDate = reqDeliveryDate;
	}
	
	public String getReqShipDate() {
		return reqShipDate;
	}

	public void setReqShipDate(String reqShipDate) {
		this.reqShipDate = reqShipDate;
	}
	
	public void setShipNode(String shipNode) {
		this.shipNode = shipNode;
	}

	public String getShipNode() {
		return shipNode;
	}
	
	public String getShipToID() {
		return shipToID;
	}

	public void setShipToID(String shipToID) {
		this.shipToID = shipToID;
	}
	
	public String getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(String timeZone) {
		this.timeZone = timeZone;
	}
	
	public String getShippingMethod() {
		return shippingMethod;
	}

	public void setShippingMethod(String shippingMethod) {
		this.shippingMethod = shippingMethod;
	}
	
	public String getStatus() {
		return SOCUtility.checkNull(status);
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	public SOCOrderLineItem getLineItem() {
		return item;
	}

	public String getItemCategory() {
		return itemCategory;
	}

	public void setItemCategory(String itemCategory) {
		this.itemCategory = itemCategory;
	}

	public void setLineItem(SOCOrderLineItem item) {
		this.item=item;
	}	
	
	public SOCOrderLinePriceInfo getLinePriceInfo() {
		return priceInfo;
	}

	public void setLinePriceInfo(SOCOrderLinePriceInfo priceInfo) {
		this.priceInfo=priceInfo;
	}	
	
	public SOCOrderPersonalInfoShipTo getInfoShipTo() {
		return infoShipTo;
	}

	public void setInfoShipTo(SOCOrderPersonalInfoShipTo infoShipTo) {
		this.infoShipTo=infoShipTo;
	}
	
	public SOCOrderNotes getLineNotes() {
		return lineNotes;
	}

	public void setLineNotes(SOCOrderNotes lineNotes) {
		this.lineNotes=lineNotes;
	}	
	
	public SOCOrderLineExtn getLineExtn() {
		return lineExtn;
	}

	public void setLineExtn(SOCOrderLineExtn lineExtn) {
		this.lineExtn=lineExtn;
	}	
	
	public SOCOrderLineStatus getLineStatus() {
		return lineStatus;
	}

	public void setLineStatus(SOCOrderLineStatus lineStatus) {
		this.lineStatus=lineStatus;
	}	
	
	public SOCOrderLineTaxes getLineTaxes() {
		return lineTaxes;
	}

	public void setLineTaxes(SOCOrderLineTaxes lineTaxes) {
		this.lineTaxes=lineTaxes;
	}	
	
	public SOCOrderLineCharges getLineCharges() {
		return lineCharges;
	}

	public void setLineCharges(SOCOrderLineCharges lineCharges) {
		this.lineCharges=lineCharges;
	}	
	
	public SOCOrderLineSalvage getOrderLineSalvage() {
		return salvageOrderLine;
	}

	public void setOrderLineSalvage(SOCOrderLineSalvage salvageOrderLine) {
		this.salvageOrderLine=salvageOrderLine;
	}	
	
	
	@Override
	public int hashCode() {
		final int prime = 35;
		int result = 1;
		result = prime * result + ((lineId == null) ? 0 : lineId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SOCOrderLine other = (SOCOrderLine) obj;
		if (lineId == null) {
			if (other.lineId != null)
				return false;
		} else if (!lineId.equals(other.lineId))
			return false;
		
		return true;
	}
	
	@Override
	public String toString()
	{
		if(priceInfo!=null)
		{
			priceInfo.setLineTotal(lineTotal.toString());			
		}
		if(lineExtn!=null)
		{
			lineExtn.setExtnAmtCollected(lineTotal.toString());
		}
		StringBuilder sb = new StringBuilder();

		sb.append(XMLTagConstants.START_TAG_ORDER_LINE);
		sb.append(SOCConstants.BLANK_STRING);

		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.DELIVERY_METHOD,getDeliveryMethod()));
		sb.append(SOCConstants.BLANK_STRING);

		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.FULLFILLMENT_TYPE,getFulfillmentType()));
		sb.append(SOCConstants.BLANK_STRING);		

		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ITEM_GROUP_CODE, getItemGroupCode()));
		sb.append(SOCConstants.BLANK_STRING);
		
		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.LINE_TYPE, getLineType()));
		sb.append(SOCConstants.BLANK_STRING);
		
		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.MIN_SHIP_BY_DATE, getMinShipByDate()));
		sb.append(SOCConstants.BLANK_STRING);
		
		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ORDERED_QTY, getOrderedQty()));
		sb.append(SOCConstants.BLANK_STRING);

		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ORG_ORDERED_QTY, getOrgOrderedQty()));
		sb.append(SOCConstants.BLANK_STRING);
		
		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.OTHER_CHARGES,getOtherCharges().toString()));
		sb.append(SOCConstants.BLANK_STRING);

		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.PRICING_DATE,getPricingDate()));
		sb.append(SOCConstants.BLANK_STRING);

		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.PRIME_LINE_NUM, String.valueOf(getPrimeLineNo())));
		sb.append(SOCConstants.BLANK_STRING);

		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.PROMISE_APP_START_DATE, getPromisedStartDate()));
		sb.append(SOCConstants.BLANK_STRING);
		
		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.PROMISE_APP_END_DATE, getPromisedEndDate()));
		sb.append(SOCConstants.BLANK_STRING);
		
		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.REQ_DELIVERY_DATE, getReqDeliveryDate()));
		sb.append(SOCConstants.BLANK_STRING);
		
		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.REQ_SHIP_DATE, getReqShipDate()));
		sb.append(SOCConstants.BLANK_STRING);
		
		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.SHIP_NODE, getShipNode()));
		sb.append(SOCConstants.BLANK_STRING);
		
		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.SHIP_TO_ID, getShipToID()));
		sb.append(SOCConstants.BLANK_STRING);
		
		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.SHIP_TO_KEY,getLineShipToKey()));
		sb.append(SOCConstants.BLANK_STRING);
				
		sb.append(SOCUtility.getAttribute(XMLAttributeConstants.TIMEZONE, getTimeZone()));
		sb.append(SOCConstants.BLANK_STRING);

		sb.append(SOCConstants.CLOSING_XML_TAG);
		
		String lineItem=getLineItem().toString();
		if(lineItem.length()>0)
		{
			sb.append(SOCConstants.NEW_LINE);
			sb.append(lineItem);
		}
		
		String linePriceInfo = getLinePriceInfo().toString();
		if(linePriceInfo.length()>0)
		{
			sb.append(SOCConstants.NEW_LINE);
			sb.append(linePriceInfo);
		}
		
		String infoShipTo = getInfoShipTo().toString();
		if(infoShipTo.length()>0)
		{
			sb.append(SOCConstants.NEW_LINE);
			sb.append(infoShipTo);
		}
		
		String lineNotes=getLineNotes().toString();
		if(lineNotes.length()>0)
		{
			sb.append(SOCConstants.NEW_LINE);
			sb.append(lineNotes);
		}
		
		String lineCharges = getLineCharges().toString();
		if(lineCharges.length()>0)
		{
			sb.append(SOCConstants.NEW_LINE);
			sb.append(lineCharges);
		}
		
		String lineTaxes=getLineTaxes().toString();
		if(lineTaxes.length()>0)
		{
			sb.append(SOCConstants.NEW_LINE);
			sb.append(lineTaxes);
		}
		
		String lineStatus=getLineStatus().toString();
		if(lineStatus.length()>0)
		{
			sb.append(SOCConstants.NEW_LINE);
			sb.append(lineStatus);
		}
		
//		String lineschedule=getLineschedule().toString();
//		if(lineschedule.length()>0)
//		{
//			sb.append(SOCConstants.NEW_LINE);
//			sb.append(lineschedule);
//		}
		
		String salvageLine=getOrderLineSalvage().toString();
		if(salvageLine.length()>0)
		{
			sb.append(SOCConstants.NEW_LINE);
			sb.append(salvageLine);
		}
		
		String lineExtn=getLineExtn().toString();
		if(lineExtn.length()>0)
		{
			sb.append(SOCConstants.NEW_LINE);
			sb.append(lineExtn);
		}
		
		sb.append(SOCConstants.NEW_LINE);
		sb.append(XMLTagConstants.END_TAG_ORDER_LINE);

		return sb.toString();
	}
	
	public void setLineTotal()
	{
		BigDecimal totalTax =new BigDecimal("0");
		BigDecimal lineAmt =new BigDecimal("0");
		BigDecimal salvageLineAmt =new BigDecimal("0");
		if(lineTaxes!= null  ){
			totalTax = lineTaxes.getTaxTotal();
		}
			
		if(priceInfo!=null)
		{
			lineAmt = priceInfo.getUnitPrice().multiply(new BigDecimal(priceInfo.getOrderedPricingQty()));
		}
		
		if(salvageOrderLine!=null)
		{
			salvageLineAmt = salvageOrderLine.getUnitPrice();
		}
		
		lineTotalTax=totalTax;
		lineTotal= totalTax.add(lineAmt);
		lineTotal = lineTotal.add(salvageLineAmt);
	}
	
	public BigDecimal getLineTotalTax()
	{
		return lineTotalTax;
	}
	
	public BigDecimal getLineTotal()
	{
		return lineTotal;
	}
}
